package test.db;
import java.sql.*;

public class MysqlDemo {
    public static void main(String[] args) {
        System.out.println("MySQL JDBC Example.");
        Connection conn = null;
        String url = "jdbc:mysql://127.0.0.1:33061/kiamp_hw841_20181130?useUnicode=true&characterEncoding=GBK&zeroDateTimeBehavior=convertToNull";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "123456";
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, userName, password);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from tb_user");   
            int c = rs.getMetaData().getColumnCount();   
            while(rs.next()){                      
                System.out.println();   
                for(int i=1;i<=c;i++){   
                    System.out.print(rs.getObject(i) + " ");   
                }   
            }   
            rs.close();   
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) { } // ignore
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException sqlEx) { } // ignore
            }
        }
    }
}